package com.mengxj;


import com.common.string.UrlHelper;

import java.util.*;

public class StringDemo implements UrlHelper {

    @Override
    public Map<String, Object> info(String url) {
        Map<String, Object> res = new HashMap<>();
        String[] strings = url.split("://");
        String line;
        if(strings.length == 1){
            line = url;
            res.put("protocol", "http");
        }else{
            line = strings[1];
            res.put("protocol", strings[0]);
        }
        int index = line.indexOf("/");
        String hostPort = line.substring(0, index);
        String uriParams = line.substring(index);
        String[] data = hostPort.split(":");
        res.put("host", data[0]);
        if(data.length == 1){
            res.put("port", 80);
        }else{
            res.put("port", data[1]);
        }
        String[] array = uriParams.split("\\?");
        res.put("uri", array[0]);
        if(array.length == 2){
            String[] params = array[1].split("&");
            List<String> paramList = new ArrayList<>();
            res.put("param", paramList);
            paramList.addAll(Arrays.asList(params));
        }
        return res;
    }

    public static void main(String[] args) {
        String url = "http://10.122.12.12:1234/api/host?name=张三&age=12&address=123";
        StringDemo demo = new StringDemo();
        Map<String, Object> res = demo.info(url);
        System.out.println(res);
    }
}
